const express = require('express');

const postService = require('../service/post');

const router = express.Router();

// 获取帖子类型
router.get('/postType', async function (req, res, next) {
    postService.getPostType().then((result) => {
        res.status(result.code).json(result);
    }).catch(err => {
        res.status(500).json({ code: 500, msg: '服务器错误', data: null });
    });
});

/**
 * @接口名称 发布动态接口
 * @用途 申请发布带有图片的动态
 * @参数 { title: string, content: string, img: string, type: string, is_anonymous: boolean }
 */
router.post('/post', async function (req, res, next) {
    let uid = req.uid;
    let title = req.body.title;
    if (!title) {
        return res.status(400).json({ code: 400, msg: '标题不允许为空', data: null });
    }
    if (title.length > 100) {
        return res.status(400).json({ code: 400, msg: '标题长度不能超过100个字符', data: null });
    }
    let content = req.body.content;
    if (!content) {
        return res.status(400).json({ code: 400, msg: '内容不允许为空', data: null });
    }
    if (content.length > 50000) {
        return res.status(400).json({ code: 400, msg: '内容长度不能超过50000个字符', data: null });
    }
    let img = req.body.img || '';
    let buffer;
    if (img) {
        // 判断图片格式是否为 base64
        if (!img.startsWith('data:image')) {
            return res.status(400).json({ code: 400, msg: '图片格式错误, 应是base64编码的dataURL数据', data: null });
        }
        // 提取 base64 数据部分 再次判空
        let base64Data = img.split(',')[1];
        if (!base64Data) {
            return res.status(400).json({ code: 400, msg: '图片格式错误, base64数据为空', data: null });
        }
        // 将 base64 数据转换为 Buffer
        buffer = Buffer.from(base64Data, 'base64');
        // 判断 img 这个base64字符串占用的字节是否超过 10M, 超过直接返回失败
        if (buffer.length > 10 * 1024 * 1024) {
            return res.status(400).json({ code: 400, msg: '图片过大，请重新上传', data: null });
        }
    }
    let type = req.body.type || null;
    postService.addPost(uid, title, content, buffer, type).then((result) => {
        res.status(result.code).json(result);
    }).catch((err) => {
        res.status(500).json((new Response()).fail("预发布动态失败"));
    });
});

// 评论帖子接口
router.post('/comment', async function (req, res, next) {
    let uid = req.uid;
    let postId = req.body.postId;
    let content = req.body.content;
    if (!postId) {
        return res.status(400).json({ code: 400, msg: '树洞ID不允许为空', data: null });
    }
    if (!content) {
        return res.status(400).json({ code: 400, msg: '内容不允许为空', data: null });
    }
    postService.addComment(uid, postId, content).then((result) => {
        res.status(result.code).json(result);
    }).catch(err => {
        res.status(500).json({ code: 500, msg: '服务器错误', data: null });
    });
});

// 懒加载获取帖子列表
router.get('/post-list', async function (req, res, next) {
    let uid = req.uid;
    let cursor = req.query.cursor || null;
    let limit = parseInt(req.query.limit) || 20;
    if (limit > 50) {
        limit = 50;
    }
    postService.getPostList(uid, cursor, limit).then((result) => {
        res.status(result.code).json(result);
    }).catch(err => {
        res.status(500).json({ code: 500, msg: '服务器错误', data: null });
    });
});

// 懒加载获取某个类型的帖子列表
router.get('/post-list-byType', async function (req, res, next) {
    let uid = req.uid;
    let typeId = req.query.id;
    let cursor = req.query.cursor || null;
    let limit = parseInt(req.query.limit) || 20;
    if (limit > 50) {
        limit = 50;
    }
    postService.getPostLikeByType(uid, typeId, cursor, limit).then((result) => {
        res.status(result.code).json(result);
    }).catch(err => {
        res.status(500).json({ code: 500, msg: '服务器错误', data: null });
    });
});

// 懒加载获取帖子评论
router.get('/post-comment', async function (req, res, next) {
    let postId = req.query.postId;
    let cursor = req.query.cursor || null;
    let limit = parseInt(req.query.limit) || 20;
    if (limit > 50) {
        limit = 50;
    }
    postService.getPostComment(postId, cursor, limit).then((result) => {
        res.status(result.code).json(result);
    }).catch(err => {
        res.status(500).json({ code: 500, msg: '服务器错误', data: null });
    });
});

// 点赞或取消点赞帖子
router.post('/like', async function (req, res, next) {
    let uid = req.uid;
    let postId = req.body.postId;
    let is_like = req.body.is_like;   // true or false
    postService.like(uid, postId, is_like).then((result) => {
        res.status(result.code).json(result);
    }).catch(err => {
        res.status(500).json({ code: 500, msg: '服务器错误', data: null });
    });
});

// 收藏或取消收藏帖子
router.post('/collect', async function (req, res, next) {
    let uid = req.uid;
    let postId = req.body.postId;
    let is_collect = req.body.is_collect;   // true or false
    postService.collect(uid, postId, is_collect).then((result) => {
        res.status(result.code).json(result);
    }).catch(err => {
        res.status(500).json({ code: 500, msg: '服务器错误', data: null });
    });
});

// 懒加载获取收藏帖子列表
router.get('/collect-post-list', async function (req, res, next) {
    let uid = req.uid;
    let cursor = req.query.cursor || null;
    let limit = parseInt(req.query.limit) || 20;
    if (limit > 50) {
        limit = 50;
    }
    postService.getCollectPostlist(uid, cursor, limit).then((result) => {
        res.status(result.code).json(result);
    }).catch(err => {
        res.status(500).json({ code: 500, msg: '服务器错误', data: null });
    });
});

// 获取贴子详情接口
router.get('/post', async function (req, res, next) {
    let uid = req.uid;
    let postId = req.query.id;
    if (!postId) {
        return res.status(400).json({ code: 400, msg: '树洞ID不允许为空', data: null });
    }
    postService.getPostDetail(uid, postId).then((result) => {
        res.status(result.code).json(result);
    }).catch(err => {
        res.status(500).json({ code: 500, msg: '服务器错误', data: null });
    });
});

// 获取 点赞、收藏、评论 未读数量
router.get('/unread-count', async function (req, res, next) {
    let uid = req.uid;
    postService.getUnreadCount(uid).then((result) => {
        res.status(result.code).json(result);
    }).catch(err => {
        res.status(500).json({ code: 500, msg: '服务器错误', data: null });
    });
});

// 所有点赞未读消息设为已读
router.put('/like-read', async function (req, res, next) {
    let uid = req.uid;
    postService.likeRead(uid).then((result) => {
        res.status(result.code).json(result);
    }).catch(err => {
        res.status(500).json({ code: 500, msg: '服务器错误', data: null });
    });
});

// 所有收藏未读消息设为已读
router.put('/collect-read', async function (req, res, next) {
    let uid = req.uid;
    postService.collectRead(uid).then((result) => {
        res.status(result.code).json(result);
    }).catch(err => {
        res.status(500).json({ code: 500, msg: '服务器错误', data: null });
    });
});

// 所有评论未读消息设为已读
router.put('/comment-read', async function (req, res, next) {
    let uid = req.uid;
    postService.commentRead(uid).then((result) => {
        res.status(result.code).json(result);
    }).catch(err => {
        res.status(500).json({ code: 500, msg: '服务器错误', data: null });
    });
});

// 懒加载未读点赞信息
router.get('/like-list', async function (req, res, next) {
    let uid = req.uid;
    let cursor = req.query.cursor || null;
    let limit = parseInt(req.query.limit) || 20;
    if (limit > 50) {
        limit = 50;
    }
    postService.getLikeListLazyLoad(uid, cursor, limit).then((result) => {
        res.status(result.code).json(result);
    }).catch(err => {
        res.status(500).json({ code: 500, msg: '服务器错误', data: null });
    });
});

// 懒加载未读收藏信息
router.get('/collect-list', async function (req, res, next) {
    let uid = req.uid;
    let cursor = req.query.cursor || null;
    let limit = parseInt(req.query.limit) || 20;
    if (limit > 50) {
        limit = 50;
    }
    postService.getCollectListLazyLoad(uid, cursor, limit).then((result) => {
        res.status(result.code).json(result);
    }).catch(err => {
        res.status(500).json({ code: 500, msg: '服务器错误', data: null });
    });
});

// 懒加载未读评论信息
router.get('/comment-list', async function (req, res, next) {
    let uid = req.uid;
    let cursor = req.query.cursor || null;
    let limit = parseInt(req.query.limit) || 20;
    if (limit > 50) {
        limit = 50;
    }
    postService.getCommentListLazyLoad(uid, cursor, limit).then((result) => {
        res.status(result.code).json(result);
    }).catch(err => {
        res.status(500).json({ code: 500, msg: '服务器错误', data: null });
    });
});

module.exports = router;